package com.ldzy.controller;

import com.ldzy.exception.LoginException;
import com.ldzy.pojo.User;
import com.ldzy.service.BookService;
import com.ldzy.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Slf4j
@RequestMapping("/users")
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    //处理登录请求
    @PostMapping("/login")
    public MyResponse login(@RequestBody User user, HttpSession session){
        User user1 = userService.login(user);//返回的user1不为空，表登录成功
        if(user1!=null){//写入session
            session.setAttribute("user",user);
            return new MyResponse(ResultCode.SUCCESS_LOGIN,user1);
        }
        return new MyResponse(ResultCode.ERROR_LOGIN,null);
    }

    @GetMapping("/logout")
    public MyResponse logout(HttpSession session){
        session.removeAttribute("user");
        return new MyResponse(ResultCode.SUCCESS);
    }
}
